<!DOCTYPE html>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script>
function testCanvas_0f0(ctx) {
    var colorData = ctx.getImageData(50, 50, 1, 1).data;
    assert_equals(colorData[0], 0);
    assert_equals(colorData[1], 255);
    assert_equals(colorData[2], 0);
    assert_equals(colorData[3], 255);
}

function testException(image) {  
    var dstCanvas = document.createElement('canvas');
    dstCanvas.width = dstCanvas.height = 100;
    var dstCtx = dstCanvas.getContext('bitmaprenderer');
    assert_true(dstCtx instanceof ImageBitmapRenderingContext);
    dstCtx.transferFromImageBitmap(image);

    // image is detached after calling transferFromImageBitmap,
    // so call transferFromImageBitmap(image) should throw InvalidStateError
    assert_throws_dom("InvalidStateError", function() {dstCtx.transferFromImageBitmap(image);});
}

function testDrawCanvasWithImageBitamp(image) {  
    var dstCanvas = document.createElement('canvas');
    dstCanvas.width = dstCanvas.height = 100;
    var dstCtx = dstCanvas.getContext('bitmaprenderer');
    assert_true(dstCtx instanceof ImageBitmapRenderingContext);
    dstCtx.transferFromImageBitmap(image);

    // Calling dstCtx.canvas should get the same canvas as the dstCanvas
    var testCanvas = dstCtx.canvas;
    assert_equals(dstCanvas.width, testCanvas.width);
    assert_equals(dstCanvas.height, testCanvas.height);

    // After transferFromImageBitmap, image's width && height should be 0
    assert_equals(image.width, 0);
    assert_equals(image.height, 0);

    var myCanvas = document.createElement('canvas');
    myCanvas.width = myCanvas.height = 100;
    var myCtx = myCanvas.getContext('2d');
    myCtx.drawImage(dstCanvas, 10, 10);
    testCanvas_0f0(myCtx);
}

promise_test(function() {
    var canvas = document.createElement('canvas');
    canvas.width = canvas.height = 100;
    var ctx = canvas.getContext('2d');
    ctx.fillStyle = '#0f0';
    ctx.fillRect(0, 0, 100, 100);
    testCanvas_0f0(ctx);

    return createImageBitmap(canvas).then(testDrawCanvasWithImageBitamp);
}, 'testDrawCanvasWithImageBitamp should not return any error');

promise_test(function() {
    var canvas = document.createElement('canvas');
    canvas.width = canvas.height = 100;
    var ctx = canvas.getContext('2d');
    ctx.fillStyle = '#0f0';
    ctx.fillRect(0, 0, 100, 100);

    return createImageBitmap(canvas).then(testException);
}, 'transferFromImageBitmap(image) with a neutered image should throw InvalidStateError');

</script>
